package com.baihan.angus.app.game.busshow.ui.script_library.script_rank;

import android.graphics.Typeface;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.Gravity;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.baihan.angus.app.game.basic.base.BaseActivity;
import com.baihan.angus.app.game.basic.configs.BasicConfig;
import com.baihan.angus.app.game.busshow.BR;
import com.baihan.angus.app.game.busshow.R;
import com.baihan.angus.app.game.busshow.databinding.ActivityScriptRankBinding;
import com.baihan.angus.app.game.common.arouter_path.ArouterAppPath;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;
import com.gyf.immersionbar.ImmersionBar;

import java.util.ArrayList;
import java.util.List;

/**
 * 剧本榜单
 */
@Route(path = ArouterAppPath.SCRIPT_RANK_ACTIVITY)
public class ScriptRankActivity extends BaseActivity<ActivityScriptRankBinding, ScriptRankVM> {

    private String[] titles = new String[]{"新品榜", "神作榜", "热销榜"};
    private List<Fragment> fragments = new ArrayList<>();

    @Override
    protected int getContentLayout() {
        return R.layout.activity_script_rank;
    }

    @Override
    protected int getViewModelId() {
        return BR.scriptRankVM;
    }

    @Override
    protected void initStatusBar() {
        ImmersionBar.with(this).fitsSystemWindows(false).transparentStatusBar().statusBarDarkFont(BasicConfig.getInstance().isDayMode()).init();
    }

    @Override
    protected void initView(Bundle savedInstanceState) {

        LinearLayout.LayoutParams top = (LinearLayout.LayoutParams) mVDBinding.statusBarFillView.getLayoutParams();
        top.height = ImmersionBar.getStatusBarHeight(this);
        mVDBinding.statusBarFillView.setLayoutParams(top);

        mVDBinding.ivBack.setOnClickListener(view -> finish());

        initViewPager();
    }

    private void initViewPager() {
        for (int i = 1; i <= titles.length; i++) {
            fragments.add(RankingFragment.newInstance(titles[i - 1]));
        }
        mVDBinding.viewPager.setAdapter(new FragmentStateAdapter(getSupportFragmentManager(), getLifecycle()) {

            @Override
            public int getItemCount() {
                return fragments.size();
            }

            @NonNull
            @Override
            public Fragment createFragment(int position) {
                return fragments.get(position);
            }
        });
        mVDBinding.viewPager.setOffscreenPageLimit(fragments.size() - 1);
        mVDBinding.viewPager.registerOnPageChangeCallback(mOnPageChangeCallback);
        new TabLayoutMediator(mVDBinding.tab, mVDBinding.viewPager, (tab, position) -> {
            //这里可以自定义TabView
            TextView tabView = new TextView(ScriptRankActivity.this);

            tabView.setText(titles[position]);
            tabView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
            tabView.setGravity(Gravity.CENTER);
            tabView.setTextColor(ContextCompat.getColor(ScriptRankActivity.this, R.color.textColor25));

            tab.setCustomView(tabView);

        }).attach();
    }

    ViewPager2.OnPageChangeCallback mOnPageChangeCallback = new ViewPager2.OnPageChangeCallback() {
        @Override
        public void onPageSelected(int position) {
            //可以来设置选中时tab的大小
            int tabCount = mVDBinding.tab.getTabCount();
            for (int i = 0; i < tabCount; i++) {
                TabLayout.Tab tab = mVDBinding.tab.getTabAt(i);
                TextView tabView = (TextView) tab.getCustomView();
                if (tab.getPosition() == position) {
                    tabView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
                    tabView.setTypeface(Typeface.DEFAULT_BOLD);
                    tabView.setTextColor(ContextCompat.getColor(ScriptRankActivity.this, R.color.bgColor));
                } else {
                    tabView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
                    tabView.setTypeface(Typeface.DEFAULT);
                    tabView.setTextColor(ContextCompat.getColor(ScriptRankActivity.this, R.color.textColor25));
                }
            }
        }
    };

    @Override
    protected void onDestroy() {
        mVDBinding.viewPager.unregisterOnPageChangeCallback(mOnPageChangeCallback);
        super.onDestroy();
    }
}